# -*- coding: utf-8 -*-
"""
    author: Ye
    date: 2021/1/28 13:49 
"""
from datetime import datetime
from extensions import db
from werkzeug.security import generate_password_hash, check_password_hash


class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(25), unique=True, nullable=True, comment='用户名')
    _password_hash_  = db.Column(db.String(255), comment='密码')
    extend = db.relationship('UserExtend', uselist=False)
    create_time = db.Column(db.DateTime, default=datetime.now)

    @property
    def password(self):
        raise Exception('密码不能被读取')

    @password.setter
    def password(self, password):
        self._password_hash_ = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self._password_hash_, password)


class UserExtend(db.Model):
    __tablename__ = 'user_extend'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    nickname = db.Column(db.String(100), nullable=False, comment='昵称')
    create_time = db.Column(db.DateTime, default=datetime.now)
